﻿Imports Microsoft.Win32

Public Class Form1

    Private c_objCoreLogic As clsCoreLogic
    Private c_strLastpath As String
    Private WithEvents bsTopGroups As BindingSource
    Private WithEvents bsSubGroups As BindingSource
    Private WithEvents bsInstruments As BindingSource

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        c_objCoreLogic = clsCoreLogic.GetSingleton
        AddHandler c_objCoreLogic.DataChanged, AddressOf ResetTree


        'dgvTopGroups.Columns.Add("tgname", "Group Name")


    End Sub

   

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        ClearForm()


    End Sub


    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click

        Dim openFileDialog1 As New OpenFileDialog()


        c_strLastpath = GetPathFromRegistry()
        If c_strLastpath Is Nothing Then
            openFileDialog1.InitialDirectory = "c:\"
        Else
            openFileDialog1.InitialDirectory = System.IO.Path.GetDirectoryName(c_strLastpath)
        End If

        openFileDialog1.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Try

                OpenLibrary(openFileDialog1.FileName)
                c_strLastpath = openFileDialog1.FileName
                SetPathInRegistry(c_strLastpath)
                'write to registry
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
            Finally
                ' Check this again, since we need to make sure we didn't throw an exception on open.

            End Try

        End If

    End Sub

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click

        ClearForm()


        c_objCoreLogic.NewLibrary()
        '  cbTopGroups.SelectedIndex = 0
        txtCurrentLibraryName.Text = "<< new library >>"
        txtCurrentLibraryName.Enabled = True
        '  nudTopGroup.Value = 1


    End Sub


    Private Sub OpenLibrary(ByVal strFileName As String)

        c_objCoreLogic.LoadFromXml(strFileName)

    End Sub

    Private Sub ResetTree()

        bsTopGroups = New BindingSource
        bsTopGroups.DataSource = c_objCoreLogic.Library.Topgroups

        dgvTopGroups.DataSource = bsTopGroups
        dgvTopGroups.Columns.Clear()
        Dim tgCol1 As New DataGridViewTextBoxColumn()
        tgCol1.DataPropertyName = "Name"
        tgCol1.HeaderText = "Group"
        dgvTopGroups.Columns.Add(tgCol1)
        dgvTopGroups.SelectionMode = DataGridViewSelectionMode.FullRowSelect

        gboxMain.Enabled = True
        txtCurrentLibraryName.Enabled = True
        txtCurrentLibraryName.Text = c_objCoreLogic.Library.Name



    End Sub


    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        Dim blnValidationFail As Boolean = False
        Dim saveFileDialog1 As New SaveFileDialog()

        If txtCurrentLibraryName.Text = "" Or txtCurrentLibraryName.Text = "<< new library >>" Then
            MsgBox("Please enter a name for your library", MsgBoxStyle.OkOnly)
            blnValidationFail = True
        End If

        If Not blnValidationFail Then

            saveFileDialog1.InitialDirectory = "c:\"
            saveFileDialog1.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*"
            saveFileDialog1.FilterIndex = 2
            saveFileDialog1.RestoreDirectory = True

            If saveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Try

                    c_objCoreLogic.SaveXml(saveFileDialog1.FileName)
                    c_strLastpath = saveFileDialog1.FileName
                    SetPathInRegistry(c_strLastpath)

                    'write to registry
                Catch Ex As Exception
                    MessageBox.Show("Cannot save file to disk. Original error: " & Ex.Message)
                Finally
                    ' Check this again, since we need to make sure we didn't throw an exception on open.

                End Try

            End If

        End If
    End Sub


    Private Sub SaveToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click

        Dim blnValidationFail As Boolean = False

        If txtCurrentLibraryName.Text = "" Or txtCurrentLibraryName.Text = "<< new library >>" Then
            MsgBox("Please enter a name for your library", MsgBoxStyle.OkOnly)
            blnValidationFail = True
        End If

        If Not blnValidationFail Then

            Try

                c_objCoreLogic.SaveXml(c_strLastpath)

                'write to registry
            Catch Ex As Exception
                MessageBox.Show("Cannot save file to disk. Original error: " & Ex.Message)
            Finally
                ' Check this again, since we need to make sure we didn't throw an exception on open.

            End Try

        End If
    End Sub

    Private Sub ClearForm()

        gboxMain.Enabled = False
        gboxSub.Enabled = False
        gboxInst.Enabled = False
        txtCurrentLibraryName.Enabled = False


        txtCurrentLibraryName.Text = ""


    End Sub


    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click


        'load the current library (if any)
        'compare agains the loaded library
        'check the same?
        MsgBox("TODO")

        If MsgBox("Close without saving?") = MsgBoxResult.Ok Then
            ClearForm()
        End If


    End Sub

    Private Sub txtCurrentLibraryName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCurrentLibraryName.TextChanged
        If txtCurrentLibraryName.Text.Length > 0 Then
            c_objCoreLogic.Library.Name = txtCurrentLibraryName.Text
        End If
    End Sub

  

    Private Sub bsTopGroups_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bsTopGroups.PositionChanged
        'get the current top group

        Dim lstSubgroups As List(Of clsSubGroup)

        If Not bsTopGroups.Current Is Nothing Then
            lstSubgroups = CType(bsTopGroups.Current, clsTopGroup).Subgroups

            bsSubGroups = New BindingSource
            bsSubGroups.DataSource = lstSubgroups

            dgvSubGroups.DataSource = bsSubGroups
            dgvSubGroups.Columns.Clear()
            Dim tgCol1 As New DataGridViewTextBoxColumn()
            tgCol1.DataPropertyName = "Name"
            tgCol1.HeaderText = "Sub Group"
            dgvSubGroups.Columns.Add(tgCol1)
            gboxSub.Enabled = True

        End If

    End Sub

   


    Private Sub bsSubGroups_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bsSubGroups.PositionChanged
        'get the current sub group

        Dim lstInstruments As List(Of clsInstrument)
        Dim tgCol1 As New DataGridViewTextBoxColumn()
        Dim tgCol2 As New DataGridViewTextBoxColumn()
        Dim tgCol3 As New DataGridViewTextBoxColumn()


        If Not bsSubGroups.Current Is Nothing Then
            lstInstruments = CType(bsSubGroups.Current, clsSubGroup).Instruments

            bsInstruments = New BindingSource
            bsInstruments.DataSource = lstInstruments

            dgvInstruments.DataSource = bsInstruments
            dgvInstruments.Columns.Clear()
            tgCol1.DataPropertyName = "Name"
            tgCol1.HeaderText = "Instrument"
            dgvInstruments.Columns.Add(tgCol1)
            tgCol2.DataPropertyName = "VSTInstrument"
            tgCol2.HeaderText = "VST"
            dgvInstruments.Columns.Add(tgCol2)
            tgCol3.DataPropertyName = "fxpPath"
            tgCol3.HeaderText = "fxp path"
            dgvInstruments.Columns.Add(tgCol3)
            gboxInst.Enabled = True

        End If


    End Sub

    Private Function GetPathFromRegistry() As String
        Dim regPath As RegistryKey
        Dim strPath As String
        Dim strRegistryPath As String
        Dim strDefaultLibraryPath As String

        strRegistryPath = Configuration.ConfigurationSettings.AppSettings("RegistryPath")
        strDefaultLibraryPath = Configuration.ConfigurationSettings.AppSettings("DefaultLibraryPath")

        regPath = Registry.CurrentUser.OpenSubKey(strRegistryPath, True)
        If regPath Is Nothing Then
            ' Key doesn't exist; create it.
            regPath = Registry.CurrentUser.CreateSubKey(strRegistryPath)
        End If

        If (Not regPath Is Nothing) Then
            strPath = regPath.GetValue("CurrentLibrary", strDefaultLibraryPath)
            regPath.Close()
        End If

        Return strPath

    End Function

    Private Sub SetPathInRegistry(ByVal strPath As String)
        Dim regPath As RegistryKey
        Dim strRegistryPath As String

        strRegistryPath = Configuration.ConfigurationSettings.AppSettings("RegistryPath")

        regPath = Registry.CurrentUser.OpenSubKey(strRegistryPath, True)
        If regPath Is Nothing Then
            ' Key doesn't exist; create it.
            regPath = Registry.CurrentUser.CreateSubKey(strRegistryPath)
        End If

        If (Not regPath Is Nothing) Then
            regPath.SetValue("CurrentLibrary", strPath)
            regPath.Close()
        End If


    End Sub



    Private Sub btnTestDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestDisplay.Click

        frmTesting.Show()


    End Sub


   
End Class
